package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        //创建shoppingcart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝属性值
        BeanUtils.copyProperties(dto, shoppingCart); //浅拷贝

        //1.判断当前菜品是否在购物车中  --条件dishId和dishFlavor和userId
        //只查当前用户自己的购物车（通过拦截器里的线程取到用户id）
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        log.info("根据用户id：{},查询到购物车的数据为：{}", BaseContext.getCurrentId(), cart);
        if(cart == null){   //代表当前菜品数据不在购物车中
            //2.补充缺失的属性值
            //判断是新增套餐还是新增菜品
            if(dto.getDishId() != null){  //代表新增的是菜品
                //根据菜品id查询菜品表
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else{                            //代表新增的是套餐
                SetmealVO setmeal = setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);  //数量，要判断购物车中是否有该菜品
            shoppingCart.setCreateTime(LocalDateTime.now());

            //3.将商品数据保存到shoppingcart表中
            shoppingCartMapper.insert(shoppingCart);
        }else {//代表当前菜品数据在购物车中
            //4.购物车中该菜品数量加1
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //获取用户id再清理
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车数据
     * @param dto
     */
    @Override
    public void delete(ShoppingCartDTO dto) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto, cart);
        cart.setUserId(BaseContext.getCurrentId());
        //重新赋值
        log.info("删除购物车数据：{}", cart);
        shoppingCartMapper.delete(cart);
    }


}
